package com.zhangzhan.gulimallcoupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.MyCollUtil;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallcoupon.dao.SeckillSessionDao;
import com.zhangzhan.gulimallcoupon.entity.SeckillSessionEntity;
import com.zhangzhan.gulimallcoupon.entity.SeckillSkuRelationEntity;
import com.zhangzhan.gulimallcoupon.service.SeckillSessionService;
import com.zhangzhan.gulimallcoupon.service.SeckillSkuRelationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Resource
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getSeckillData() {
        //根据活动场次的创建时间进行查询，查询活动前三天数据
        List<SeckillSessionEntity> sessions = this.list(Wrappers.<SeckillSessionEntity>lambdaQuery()
                .between(SeckillSessionEntity::getStartTime, startTime(), endTime()));

        //存入这场活动秒杀的商品信息
        if (MyCollUtil.isNotEmpty(sessions)) {
            sessions.forEach(session -> {
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(Wrappers.<SeckillSkuRelationEntity>lambdaQuery()
                        .eq(SeckillSkuRelationEntity::getPromotionSessionId, session.getId()));
                if (MyCollUtil.isNotEmpty(relationEntities)) {
                    session.setSkuItems(relationEntities);
                }
            });
            return sessions;
        }

        return null;
    }

    private String startTime() {
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(date, time);
        return startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String endTime() {
        LocalDate date = LocalDate.now();
        date = date.plusDays(2);
        LocalTime time = LocalTime.MAX;
        LocalDateTime endTime = LocalDateTime.of(date, time);
        return endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}